﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Pg.BioMedics.SDR.Entities;
using Pg.BioMedics.SDR.Entities.Codes;
using Pg.BioMedics.SDR.Services.Thesaurus;

namespace Pg.BioMedics.SDR.Services
{
    /// <summary>
    /// Provides utility methods supporting text query processing such as 
    /// tokens extraction from the query.
    /// </summary>
    internal static class TextQueryHelper
    {
        #region Static methods

        /// <summary>
        /// Extracts list of tokens from the provided text query
        /// </summary>
        /// <param name="textQuery">Text query</param>
        /// <returns>List of tokens</returns>
        internal static IList<Token> ExtractTokens(string textQuery)
        {
            if (String.IsNullOrEmpty(textQuery))
                return new List<Token>();

            string[] tokenStrings = textQuery.Split(' ');
            string[] distinctTokenStrings = tokenStrings.Distinct().ToArray();
            int index = 0;
            return (from tokenString in tokenStrings
                    let occurances = (from occuranceTokenString in tokenStrings
                                      where occuranceTokenString == tokenString
                                      select occuranceTokenString).Count()
                    let offset = index++
                    select new Token()
                    {
                        Value = tokenString,
                        Offset = offset,
                        Probability = (double)occurances / (double)tokenStrings.Length,
                        AcousticScore = 0, 
                        TypeCode = TokenTypeCodes.Word
                    }).ToList();
        }

        #endregion
    }
}
